var config = require('./config.js')

// const baseUrl = "https://ceshi.com/up/" //公共路径
const baseUrl = config.default.baseUrl

// var requestType = "GET | POST";
var requestType = "POST";

// 拦截返回值
function requestStop(data) {
	// 校验是否需要登录
	if (data.code == 401) {
		uni.showToast({
			title: data.msg,
			icon: 'none'
		});
		setTimeout(function() {
			uni.reLaunch({
				url: '/pages/login/login'
			});
		}, 1000)
		return false
	}
	
	console.log( JSON.stringify( data ) );
	// 统一非200返回
	if (data.code != 200) {
		uni.showToast({
			title: data.msg,
			icon: 'none'
		});
		return false
	}
	return true
}


function requestData(url, data) {
	var promise = new Promise((resolve, reject) => {
		var that = this;
		uni.request({
			url: baseUrl + url,
			data: data,
			method: requestType,
			dataType: 'json',
			header: {
				'content-type':'application/x-www-form-urlencoded',
				// 'content-type': 'application/json',
				// 'token': uni.getStorageSync('userToken')
			},
			success: function(res) {
				if (res) {
					switch (res.statusCode) {
						case 404:
							resolve('404，请求的资源不存在');
							break;
						case 500:
							resolve('500，内部服务器错误');
							break;
						default:
							if ( requestStop(res.data) )
								resolve(res.data.data);
					}
				}
			},
			error: function(e) {
				reject('网络出错');
			}
		})
	});
	return promise;
}


// post请求封装 可以传入methodType类型PUT
function autoRequest(url, data, methodType = "POST") {
	var promise = new Promise((resolve, reject) => {
		var that = this;
		var postData = data;
		uni.request({
			url: baseUrl + url,
			data: postData,
			method: methodType,
			// method: 'POST',
			dataType: 'json',
			header: {
				'content-type': 'application/x-www-form-urlencoded',
				// 'content-type': 'application/x-www-form-urlencoded',
				// 'content-type': 'application/json',
				// 'content-type': 'application/text',
				// 'token': uni.getStorageSync('userToken')
			},
			success: function(res) {
				if (res) {
					switch (res.statusCode) {
						case 404:
							resolve('404，请求的资源不存在');
							break;
						case 500:
							resolve('500，内部服务器错误');
							break;
						default:
							if ( requestStop(res.data) )
								resolve(res.data);
					}
				}
			},
			error: function(e) {
				reject('连接服务器失败!');
			}
		})
	});
	return promise;
}


// get请求封装
function getRequest(url, data) {
	var promise = new Promise((resolve, reject) => {
		var that = this;
		var postData = data;
		uni.request({
			url: baseUrl + url,
			data: postData,
			method: 'GET',
			header: {
				'content-type':'application/x-www-form-urlencoded',
				// 'content-type': 'application/json',
				// 'token': uni.getStorageSync('userToken')
			},
			success: function(res) {
				if (res) {
					switch (res.statusCode) {
						case 404:
							resolve('404，请求的资源不存在');
							break;
						case 500:
							resolve('500，内部服务器错误');
							break;
						default:
							if ( requestStop(res.data) )
								resolve(res.data);
					}
				}
			},
			error: function(e) {
				reject('网络出错');
			}
		})
	});
	return promise;
}


module.exports = {
	requestData
}
